import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/classes_config_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/scheduling_plan_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';

class DutyRecordsListController extends GetxController {
  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索字符串
  String? search;

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 班次列表
  List<ClassesConfigEntity> classesList = [];

  /// 选中的班次类型
  String? onDutyType;

  /// 选中的人员类型
  String? personType;

  /// 选中的人员状态类型
  String? dutyResults;

  /// 选择的排班开始时间
  String? classesBeginDate;

  /// 选择的排班结束时间
  String? classesEndDate;

  /// 电话号码
  List<String> phoneList = [];

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 获取班次列表
    classesList = await getDetailsData();
    // 获取值班状态类型字典
    final List<GetDictAllEntity> dutyDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'zblx_uMw8QffD');
    // 添加班次筛选条件
    final List<FilterEntitiesBean> classesFilterList = [];
    for (var item in classesList) {
      classesFilterList.add(FilterEntitiesBean(name: item.classesName, value: item.id));
    }

    // 添加状态筛选条件
    final List<FilterEntitiesBean> dutyResultsList = [];
    for (var item in dutyDictList) {
      dutyResultsList.add(FilterEntitiesBean(name: item.getShowName(), value: item.getId()));
    }

    // 添加时间类型
    final List<FilterEntitiesBean> timeList = [];
    timeList.add(FilterEntitiesBean(name: '开始时间', value: classesBeginDate));
    timeList.add(FilterEntitiesBean(name: '结束时间', value: classesEndDate));

    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '班次类型', filterList: classesFilterList);
    final FilterBean filterBean1 = FilterBean(title: '值班状态', filterList: dutyResultsList);
    final FilterBean filterBean2 = FilterBean(title: '起止时间', filterList: timeList);

    filterList.add(filterBean);
    filterList.add(filterBean1);
    filterList.add(filterBean2);
  }

  /// 获取班次列表
  Future<List<ClassesConfigEntity>> getDetailsData() async {
    final res = await HttpUtil().generateGetRequest<List<ClassesConfigEntity>>(Interface.getClassesConfigList).execute();
    List<ClassesConfigEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
      update();
    });
    return data ?? [];
  }

  /// 获取数据
  Future<List<SchedulingPlanEntity>> getListData(int pageNum, int pageSize) async {
    // 获取列表接口
    final res = await HttpUtil().generateGetRequest<List<SchedulingPlanEntity>>(
      Interface.getSchedulingConfigPlanRecordPage,
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'userName': search,
        'classesId': onDutyType,//班次
        'dutyResults': dutyResults,
        'projectBeginDate': classesBeginDate,
        'projectEndDate': classesEndDate
      },
    ).execute();
    List<SchedulingPlanEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  } // 获取列表数据

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    onDutyType = null;
    personType = null;
    dutyResults = null;
    classesBeginDate = null;
    classesEndDate = null;
    for (var i in value) {
      if (i.title == '起止时间') {
        for (var item in i.filterList) {
          switch (item.name) {
            case '开始时间':
              if (item.value != null && item.value != '') {
                classesBeginDate = item.value as String;
              }
              break;
            case '结束时间':
              if (item.value != null && item.value != '') {
                classesEndDate = item.value as String;
              }
              break;
          }
        }
      }
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '班次类型') {
        onDutyType = i.chosenValue[0].value as String;
      }
      if (i.title == '人员类型') {
        personType = i.chosenValue[0].value as String;
      }
      if (i.title == '值班状态') {
        dutyResults = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
